const path = require("path");
const webpack = require("webpack");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const WorkboxPlugin = require("workbox-webpack-plugin");

//去除多余css
const glob = require("glob");
const PurifyCSSPlugin = require("purifycss-webpack");

module.exports = {
  mode: "development",
  devtool: "cheap-module-eval-source-map",
  performance: false,
  entry: {
    main: "./webpack13-workbox/src/index.js"
  },

  devServer: {
    contentBase: "./dist",
    open: true,
    port: 8080,
    hot: true
  },

  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: "babel-loader"
      },
      {
        test: /\.css$/,
        use: [MiniCssExtractPlugin.loader, "css-loader"]
      }
    ]
  },
  plugins: [
    new CleanWebpackPlugin(),
    new HtmlWebpackPlugin({
      template: "webpack13-workbox/src/template.html"
    }),
    new webpack.HotModuleReplacementPlugin(),
    new MiniCssExtractPlugin({
      filename: "[name].css",
      chunkFilename: "[name].chunk.css"
    }),
    new PurifyCSSPlugin({
      paths: glob.sync(path.join(__dirname, "src/*.html"))
    }),
    new webpack.ProvidePlugin({
      $: "jquery",
      _: "lodash"
    }),
    new WorkboxPlugin.GenerateSW({
      clientsClaim: true,
      skipWaiting: true
    })
  ],

  optimization: {
    minimize: true,
    minimizer: [new OptimizeCSSAssetsPlugin({})],

    usedExports: true,
    runtimeChunk: {
      name: "runtime"
    },
    splitChunks: {
      chunks: "all"
    }
  },
  output: {
    filename: "[name].[hash].js",
    chunkFilename: "[name].[hash].js",
    path: path.resolve(__dirname, "dist")
  }
};
